﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Nate_sRoguelikeGameXNA.GameObjects.Architecture;
using Nate_sRoguelikeGameXNA.Levels;

namespace Nate_sRoguelikeGameXNA
{
    internal class IngameScreen : Screen
    {
        public Player Player;
        public List<Level> Levels = new List<Level>();
        public Level CurrentLevel;
        public Random Random = new Random();
        public int CurrentTurn { get; private set; }
        private QueuedInput queuedInput = null;
        static bool _quitGame = false;
        public Point DrawingOffset = new Point(0, 2);

        private Level _level;
        private Message _message;
        public IngameScreen(SpriteBatch spriteBatch)
            : base(spriteBatch)
        {

        }

        #region Overrides of Screen

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Draw(GameTime gameTime)
        {
            _spriteBatch.Begin();
            if (_message != null)
                _message.Draw(_spriteBatch);
            _level.Draw(_spriteBatch);
            _spriteBatch.End();
        }

        protected override bool GetInput(GameTime currentGameTime)
        {
            bool result = base.GetInput(currentGameTime);
            if (result)
                Player.ScanArea();
            return result;

        }

        protected override void HandleInput()
        {
            switch (_lastKeystroke.Key)
            {
                case Keys.Space:
                    _level = Level.GenerateLevel2("Level1", new Rectangle(0, 0, 80, 32), GameArchitecture.StoneWall, GameArchitecture.StonePath, GameArchitecture.StoneWall,
                                                       false, true,
                                                       null);
                    break;
                case Keys.Z:
                    _level.Corridors.First().FindPath();
                    break;
                case Keys.X:
                    Game.Debug = !Game.Debug;
                    break;
                case Keys.Q:
                    Environment.Exit(0);
                    return;
                case Keys.NumPad4:
                case Keys.NumPad7:
                case Keys.NumPad8:
                case Keys.NumPad9:
                case Keys.NumPad6:
                case Keys.NumPad3:
                case Keys.NumPad2:
                case Keys.NumPad1:
                case Keys.NumPad5:
                    Player.Move(GameDirection.GetDirection(_lastKeystroke.Key));
                    break;
                case Keys.W:
                    if (_lastKeystroke.IsShiftPressed)
                    {
                        // Wear item
                    }
                    else
                    {
                        _message = new Message() { Text = "Which direction? (47896321) ", Color = Color.White, Location = Vector2.Zero };
                        _inputHandler = QueueDirection;
                    }
                    break;
            }
        }

        public override void Initialize()
        {
            Player = new Player("Nate", CharacterClass.Elementalist);
            _level = Level.GenerateLevel2("Level1", new Rectangle(0, 0, 80, 32), GameArchitecture.StoneWall, GameArchitecture.StonePath, GameArchitecture.StoneWall, false, true,
                                              null);
        }

        public void QueueDirection()
        {
            queuedInput = new QueuedInput(_lastKeystroke, -1);
            _message = null;
            _inputHandler = HandleInput;
        }

        public void PromptForDirection()
        {

        }
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if (Player.IsDisturbed || (queuedInput != null && queuedInput.HasExpired))
                queuedInput = null;
            if (queuedInput != null)
                _inputHandler();
            else
                GetInput(gameTime);

        }

        #endregion
    }
}
